class RentsController < ApplicationController
  # GET /rents
  # GET /rents.json
  def index
    @rents = Rent.where("returned = ?", 0)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @rents }
    end
  end

  # GET /rents/returned
  # GET /rents/returned.json
  def returned_index
    @rents = Rent.where("returned = ?", 1)
    render 'index'
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @rents }
    end
  end

  # GET /rents/1
  # GET /rents/1.json
  def show
    @rent = Rent.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @rent }
    end
  end

  # GET /rents/new
  # GET /rents/new.json
  def new
    @rent = Rent.new
    @customers = Customer.all
    @products = Product.where("rented = ?", 0)

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @rent }
    end
  end

  # GET /rents/1/edit
  def edit
    @rent = Rent.find(params[:id])
  end

  # GET /rents/1/return
  def return
    @rent = Rent.find(params[:id])
  end

  #GET /rents/1/returned
  def rent_returned
    @rent = Rent.find(params[:id])
    @rent.returned = 1
    @rent.invoice = @rent.id
    @product = Product.find(@rent.product_id)
    
    respond_to do |format|
      if @rent.save then
        @product.rented = 0
        if @product.save then
          format.html { redirect_to rents_url, notice: 'Rent was successfully returned.' }
          format.json { head :no_content }
        end
      else
        format.html { render action: "return" }
        format.json { render json: @rent.errors, status: :unprocessable_entity }
      end
    end
  end

  # POST /rents
  # POST /rents.json
  def create
    @rent = Rent.new(params[:rent])
    
    #error validation
    if(params[:rent][:product_id]=="") then
      param_error = true
    end
    if(params[:rent][:customer_id]=="") then
      param_error = true
    end

    if (!param_error)
      @product = Product.find(params[:rent][:product_id])
    end

    respond_to do |format|
      if @rent.save
        #write the product as rented
        if (!param_error) then
          @product.rented = 1
          @product.save
        end
        format.html { redirect_to @rent, notice: 'Rent was successfully created.' }
        format.json { render json: @rent, status: :created, location: @rent }
      else
        @customers = Customer.all
        @products  = Product.where("rented = ?", 0)
        format.html { render action: "new" }
        format.json { render json: @rent.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /rents/1
  # PUT /rents/1.json
  def update
    @rent = Rent.find(params[:id])

    respond_to do |format|
      if @rent.update_attributes(params[:rent])
        format.html { redirect_to @rent, notice: 'Rent was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @rent.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /rents/1
  # DELETE /rents/1.json
  def destroy
    @rent = Rent.find(params[:id])
    @product = Product.find(@rent.product_id)
    @product.rented = 0
    @product.save!
    @rent.destroy

    respond_to do |format|
      format.html { redirect_to rents_url }
      format.json { head :no_content }
    end
  end




end
